package com.realgodo.site.web;

import java.util.Date;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springside.modules.web.struts2.Struts2Utils;

import com.opensymphony.xwork2.ActionSupport;
import com.realgodo.site.common.MailService;
import com.realgodo.site.entity.security.User;
import com.realgodo.site.service.UserManager;

@Results( {
		@Result(name = "user_index", type = "redirect", location = "${base}/index"),
		@Result(name = "took", type = "redirect", location = "${base}/regist!ok") })
@Scope("prototype")
public class RegistController extends ActionSupport {

	private static final long serialVersionUID = 8493113339938551756L;

	private String username; // 登陆名
	private String email;// 电子邮件

	private User user;
	private Long id;

	private String activeCode;
	@Autowired
	private MailService mailService;
	@Autowired
	private UserManager userManager;

	public String index() throws Exception {
		return SUCCESS;
	}

	/** 注册用户 */
	public String regist() {

		String uncodePassword = user.getPassword();
		user.setEnable(User.DISENABLED);
		user.setActiveCode(String.valueOf(new Date().getTime()));
		user = this.userManager.createUser(user);
		user.setPassword(uncodePassword);
		// mailService.sendRejistMail(user);// 发送等待认证通知信;
		user = userManager.get(user.getId());
		this.mailService.sendAuthMail(user);
		return "took";
	}

	/**
	 *创建用户
	 * 
	 * @return
	 */
	public String save() {
		user.setEnable(User.DISENABLED);
		user.setActiveCode(String.valueOf(new Date().getTime()));
		String password = user.getPassword();
		user = this.userManager.createUser(user);
		user.setPassword(password);
		mailService.sendRejistMail(user);
		return "user_index";
	}

	public String ok() {
		return "ok";
	}

	/** 检查email是否存在 */
	public String checkUserEmail() {
		Struts2Utils.renderText(String.valueOf(userManager
				.checkUserEmail(email)));
		return null;
	}

	/** 检查username是否存在 */
	public String checkUserName() {
		Struts2Utils.renderText(String.valueOf(userManager
				.checkUserName(username)));
		return null;
	}

	/** 激活用户 */
	public String active() {
		if (null != getId()) {
			user = this.userManager.activeUser(id, activeCode);
			if (user == null) {
				return "error";
			}
			return "user_index";
		}
		return "error";
	}

	// --------get/set-------//
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getActiveCode() {
		return activeCode;
	}

	public void setActiveCode(String activeCode) {
		this.activeCode = activeCode;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}
